#include <sys/types.h>
#include <sys/times.h>
#include <utime.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>

//enum sigNumber{SIGUSR1=0};
enum secondInterval{SECOND=1};
enum loopStat{FALSE=0,TRUE=1};
enum SIGLIST{sig0=SIGUSR1,sig1=SIGUSR2}signals;
#define NUMOFSIG 2
static unsigned int automateStat=1;

void sigintHandler(int signo,siginfo_t *info,void *context)
{
	automateStat=0;
}
void spawn(void)
{
//	struct tms automateTime;
	struct sigaction act, oldact;
	struct timeval tm;
	struct tms buf;
	clock_t start,end;
	tm.tv_sec=SECOND;
	tm.tv_usec=0;
	start=times(&buf);
	srand(1);
	if(-1 == sigaction (SIGUSR1, NULL, &oldact))
	{
		perror("spawn:sigaction");
		exit(1);
	}
  	act.sa_flags = 0;
  	sigemptyset(&act.sa_mask);
  	act.sa_handler = SIG_IGN;
 	sigprocmask(SIG_BLOCK,&(act.sa_mask),NULL);
 	/*signal SIG_IGN list*/
 	sigaction (SIGUSR1, &act, &oldact);
 	sigaction (SIGUSR2, &act, &oldact);
 	/*signal that automate doesn't send*/ 
 	act.sa_flags = SA_SIGINFO;
 	act.sa_sigaction = sigintHandler;
 	sigaction (SIGINT, &act, NULL);

	while (automateStat){
		sleep(tm.tv_sec);
		switch(rand()%NUMOFSIG){
			case 0:
				kill(getppid(),sig0);
				break;
			case 1:
				kill(getppid(),sig1);
				break;
			default:
				kill(getppid(),sig0);
				break;
		}
	}
	end=times(&buf);
	fprintf(stdout,"automate run time:%e\n",(double)((end-start)/sysconf(_SC_CLK_TCK)));
}
